// pages/follow/addcostumer/add.js
const net = require("../../../utils/net.js");
const gp = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:"",
    mobile:"",
    idcard:"",
    mark:"",
    sex:1,
    source:[
      "会籍派单","已有客户推荐","市场活动","陌生来访"
    ],
    index:0,
    textareaw:100
  },
  input(e){
    var value = e.detail.value + "";
    var key = e.currentTarget.id + "";
    this.setData({
      [key]: value,
    });
  },
  pickerChange(e){ 
    this.setData({
      index: e.detail.value
    })
  },
  sexchange(e){
    this.setData({
      sex:e.target.dataset.val||1
    })
  },
  save(){
    var dt =this.data;
    var that = this;
    if (dt.mobile.length!=11){
      that.showtip(2,"手机格式不正确");return;
    }
    if (dt.name.length ==0) {
      that.showtip(2, "请输入姓名"); return;
    }
    if (dt.idcard.length != 18 && dt.idcard.length != 15) {
      that.showtip(2, "身份证格式不正确"); return;
    }
    wx.showLoading({})
    net.request({
      url: '/AddMember',
      data:{
        name:dt.name,
        sex:dt.sex,
        mobile:dt.mobile,
        id_card:dt.idcard,
        descs:dt.mark,
        come_from:dt.source[dt.index],
        id: gp.id,
        shop_id: gp.shop_id,
        addr:"",
        occupation:gp.position
      },success(res){
        var dt = res.data;
        console.log(dt);
        if (dt.flag){
          that.showtip(3,"添加成功");
          wx.setStorageSync("followdatachange", true);
          wx.setStorageSync("managerdatachange", true);
          // let obj = wx.getStorageSync("FollowUpObj");
          // obj.mc_sell_followup_today = obj.mc_sell_followup_today*1+1;
          // wx.setStorageSync("FollowUpObj",obj);
          setTimeout(function(){
            wx.navigateBack({})
          },100)
        }else{
          that.showtip(1,dt.msg);
        }
      },fail(res){
        that.showtip(1, "添加失败");
      },complete(){
        wx.hideLoading();
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var res = wx.getSystemInfoSync();
    this.setData({
      textareaw:res.windowWidth-100
    })
    console.log(res);
  },
  showtip(lv, msg) {
    this.setData({
      tipno: lv,
      tipmsg: msg
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})